package yamlApply

import (
	"context"
	"gitee.com/Frank098/devops-app-mgt/model"
	"gitee.com/Frank098/devops-app-mgt/service"
	corev1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// CreateService 创建自定义服务
func CreateService(name, ns string, label map[string]string, spec corev1.ServiceSpec) (*corev1.Service, error) {
	svc := corev1.Service{
		TypeMeta:   metav1.TypeMeta{Kind: "service", APIVersion: "v1"},
		ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: ns, Labels: label},
		Spec:       spec,
	}
	create, err := service.K8s.ClientSet.CoreV1().Services(ns).Create(context.TODO(), &svc, metav1.CreateOptions{})
	if err != nil {
		return nil, err
	}
	return create, err
}

// UpdateService 更新service
func UpdateService(svc *corev1.Service) (*model.Response, error) {
	_, err := service.K8s.ClientSet.CoreV1().Services(svc.Namespace).Update(context.TODO(), svc, metav1.UpdateOptions{})
	if err != nil {
		return nil, err
	}
	return &model.OK, nil
}
